//
//  NSURLRequest+RequestWithParameters.m
//  WeatherApp
//
//  Created by Yuliya Grasevych on 23.07.14.
//  Copyright (c) 2014 Yuliya Grasevych. All rights reserved.
//

#import "NSURLRequest+RequestWithParameters.h"

@implementation NSURLRequest (RequestWithParameters)

+(id)requestWithURL:(NSURL *)URL parameters:(NSDictionary *)parameters
{
    NSMutableString *compiledURLString = [NSMutableString stringWithString:URL.absoluteString];
    if (parameters.count > 0) {
        
        [compiledURLString appendString:@"?"];
        [parameters enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
            
            [compiledURLString appendFormat:@"%@=%@&", key, obj];
        }];
        [compiledURLString deleteCharactersInRange:NSMakeRange(compiledURLString.length-1, 1)];
    }
    NSURL *compiledURL = [NSURL URLWithString:[compiledURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:compiledURL];
    return request;
}
@end
